<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20220315
 * Time: 11:05 
 * 用户角色模块
 */
namespace app\admin\model;

use app\admin\model\Model;
use Mscore\Core\Redis;
use Mscore\Core\Lib\SequenceNumber;
use Mscore\Core\Lib\Upload;

class Web extends Model{
	
    private $basics = 'web_basics';
    private $advert = 'web_advert';
    private $advertclass = 'web_advertclass';
    private $menu = 'web_menu';
    private $single ='web_single';
    private $contractcate ='sys_contractcate';
    private $articleclass ='sys_articleclass';

    /**
     * 基础信息设置
     * @param array $basicsset 
     * @param array $where 
     */ 
    public function basicsset($id=null){
         return Model::name($this->basics)->where([
            'id'=>$id
        ])->find();
    }

    /**
     * 基础信息
     * @param array $basics
     * @param array $where
     */
    public function getconfig($where='')
    {
        $basics = Model::name($this->basics)
            ->field('*')
            ->where($where)
            ->find();
        return $basics;
    }

    /**
     * 基础信息保存
     * @param array $basicssave 
     * @param array $where 
     */ 
    public function basicssave($data)
    {
        $id = !empty($data['id'])?$data['id']:false;
        unset($data['id']);
        if ($id) {
            Model::name($this->basics)
            ->where(' id=' . $id)
            ->update($data);
            return true;   
        }
        return fasle;
    }

    /**
     * 广告列表
     * @param array $advertlist 
     * @param array $where 
     */ 
    public function advertlist($where=null,$page=1,$page_size=10){

        //广告数据
        $advert['datalist'] = Model::name($this->advert)
            ->alias('a')
            ->join($this->advertclass.' b','a.cid=b.id','left')
            ->field(' a.*, b.title as ctitle, b.advcode')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('id desc')
            ->select()
            ->toArray();
        //数据总数
        $advert['total']=Model::name($this->advert)
            ->where($where)
            ->count('id');
        return $advert; 
    }

    /**
     * 广告添加
     * @param array $advertadd 
     * @param array $where 
     */ 
    public function advertadd($id=null){
        $info = Model::name($this->advert)->where([
            'id'=>$id
        ])->find();
        $info['langinfo']=[];
        if(isset($info['introduction'])){
            $info['introduction']=json_decode($info['introduction'],true);  
        }
        if(isset($info['chaintitle'])){
            $info['chaintitle']=json_decode($info['chaintitle'],true);  
        }

        if(isset($info['titles'])){
            $info['titles']=json_decode($info['titles'],true);
            foreach ($info['titles'] as $key => $value) {
                 $info['langinfo'][$key]['titles']=$value;
                 $info['langinfo'][$key]['introduction']=$info['introduction'][$key];
                 $info['langinfo'][$key]['chaintitle']=$info['chaintitle'][$key];
             } 
             
        }
        unset($info['introduction'],$info['chaintitle'],$info['titles']);
        // print_r($info);die;
        return $info;
    }

    /**
     * 获取广告详情
     * @param array $advertadd 
     * @param array $where 
     */ 
    public function getadvert($id=null,$file='*'){
        $info = Model::name($this->advert)
            ->field($file)
            ->where(['id'=>$id])
            ->find();
        return $info;
    }

    /**
     * 广告保存
     * @param array $advertsave 
     * @param array $where 
    */
    public function advertsave($data){
        //获取封面图片
        $advert=[];
        if(isset($data['id'])&&!empty($data['id'])){
            $advert = $this->getadvert($data['id'],'imgurl');
        }
        switch (true) {
            case isset($data['imgOne'])&&!empty($data['imgOne']):
                $url="/static/upload/category/";
                $imgurl = (new Upload)->Uploadbase64s($data['imgOne'],$url,time());
                $imgurl = implode(',',$imgurl);
                $advert['imgurl']=explode(",", $advert['imgurl']);
                if(isset($advert['imgurl'])&&is_array($advert['imgurl'])){
                    foreach ($advert['imgurl'] as $key => $value) {
                        if(file_exists(config('file.path') . $value)){
                            unlink (config('file.path') . $value); 
                        }
                    }
                }                
                break;
            default:
                $imgurl=isset($advert['imgurl'])?$advert['imgurl']:'';
                break;
        }
        $titles=[];
        $introduction=[];
        $chaintitle=[];
        if(isset($data['langs'])&&!empty($data['langs'])){
            foreach ($data['langs'] as $key => $value) {
                $titles[$value]=$data['titles'][$key];
            }
            foreach ($data['langs'] as $key => $value) {
                $introduction[$value]=$data['introduction'][$key];
            }
            foreach ($data['langs'] as $key => $value) {
                $chaintitle[$value]=$data['chaintitle'][$key];
            }
        };
        $id = !empty($data['id'])?$data['id']:false;
        $data = [
            'title'=>$data['title'],
            'titles'=>json_encode($titles,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'cid' =>$data['cid'],
            'introduction' =>json_encode($introduction,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'imgurl'=>$imgurl,
            'chaintitle'=>json_encode($chaintitle,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'chainurl'=>$data['chainurl'],
            'sortid'=>$data['sortid'],
            'status'=>$data['status'],
        ];
        switch ($id) {
            case true:
                Model::name($this->advert)->where(' id=' . $id)->update($data);
                return true;
                break;
            
            default:
                Model::name($this->advert)->insert($data);
                return true;
                break;
        }
        return fasle;
    }
    /**
     * index删除
     * @param array $del 
     * @param array $where 
     */ 
    public function del($id){ 
        //删除
        if ($id) {
            Model::name($this->advert)->where('id='.$id)->delete();
            return true;
        }
        return false;
    }

    /**
     * 广告类别添加
     * @param array $advertclassadd 
     * @param array $where 
     */ 
    public function advertclassadd($id=null){
        if($id){
         return Model::name($this->advertclass)
        ->where(['id'=>$id])
        ->find(); 
        }
        return $this->error;  
    }

    /**
     * 广告类别列表
     * @param array $advertclasslist 
     * @param array $where 
     */ 
    public function advertclasslist($where=null,$page=1,$page_size=10){

        //广告数据
        $advertclass['datalist'] = Model::name($this->advertclass)
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('id desc')
            ->select()
            ->toArray();
        //数据总数
        $advertclass['total']=Model::name($this->advertclass)
            ->where($where)
            ->count('id');
        return $advertclass; 
   
    }

    /**
     * 广告类别保存
     * @param array $catesave 
     * @param array $where 
    */
    public function catesave($data){

        $id = !empty($data['id'])?$data['id']:false;
        unset($data['id']);
        switch ($id) {
            case true:
                Model::name($this->advertclass)->where(' id=' . $id)->update($data);
                return true;
                break;
            
            default:
                Model::name($this->advertclass)->insert($data);
                return true;
                break;
        }
        return false; 
    
    }

    /**
     * 广告类别删除
     * @param array $catedel 
     * @param array $where 
     */ 
    public function catedel($id){ 

        if ($id) {
            Model::name($this->advertclass)->where('id='.$id)->delete();
            return true;
        }
        return false; 
    }

    /**
     * 菜單列表
     * @param array $menuList 
     * @param array $where 
     */ 
    public function mue($where=null){
        $menulist = Model::name($this->menu)
            ->where($where)
            ->order('orderid asc, id desc')
            ->select()
            ->toArray();
        return $menulist;
    } 
    /**
     * 菜單添加
     * @param array $menuadd 
     * @param array $where 
     */ 
    public function mueadd($id=null,$field='*'){
        $info = Model::name($this->menu)
            ->field($field)
            ->where(['id'=>$id])
            ->find(); 
        if(isset($info['titles'])){
            $info['titles']=json_decode($info['titles'],true);  
        }
        if(isset($info['param'])){
            $info['param']=json_decode($info['param'],true);
            if(!empty($info['param'])){
                foreach ($info['param'] as $key => $value) {
                    $info['param'][$key] = $key.':'.$value;
                }
                $info['param']=implode(',',$info['param']);
            } 
        }
        return $info;
    }

    /**
     * 菜單保存
     * @param array $menusave 
     * @param array $where 
     */ 
    public function muesave($data){
        //获取封面图片
        $mue=[];
        if(isset($data['id'])&&!empty($data['id'])){
            $mue = $this->mueadd($data['id'],'icon');
        }
        switch (true) {
            case isset($data['imgOne'])&&!empty($data['imgOne']):
                $url="/static/upload/mue/";
                $imgurl = (new Upload)->Uploadbase64($data['imgOne'],$url,time());
                if(isset($mue['icon'])&&file_exists(config('file.path') . $mue['icon'])){
                    unlink (config('file.path') . $mue['icon']); 
                }
                break;
            default:
                $imgurl=isset($mue['icon'])?$mue['icon']:'';
                break;
        }
        $langs=[];
        if(isset($data['langs'])&&!empty($data['langs'])){
            foreach ($data['langs'] as $key => $value) {
                $langs[$value]=$data['titles'][$key];
            }
        };
        if($data['param']){
           $data['param']=explode(',',$data['param']);
           if(!empty($data['param']))
           foreach ($data['param'] as $key => $value) {
                $value=explode(':',$value);
                $data['param'][$value[0]]=$value[1];
                unset($data['param'][$key]);
           }
        }
        $id = !empty($data['id'])?$data['id']:false;
        $data = [
            'title'=>$data['title'],
            'titles'=>json_encode($langs,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'url' =>$data['url'],
            'pid' =>$data['pid'],
            'icon'=>$imgurl,
            'is_icon'=>$data['is_icon'],
            'orderid'=>$data['orderid'],
            'is_target'=>$data['is_target'],
            'special'=>$data['special'],
            'status'=>$data['status'],
            'acode'=>$data['acode'],
            'pagecode'=>!empty($data['pagecode'])?$data['pagecode']:$data['acode'],
            'param'=>!empty($data['param'])?json_encode($data['param']):null
        ];
        switch ($id) {
            case true:
                Model::name($this->menu)->where(' id=' . $id)->update($data);
                return true;
                break;
            default:
                Model::name($this->menu)->insert($data);
                return true;
                break;
        }
        return fasle;
    }

    /**
     * 菜單删除
     * @param array $menudel 
     * @param array $where 
     */ 
    public function muedel($id=null){
        //删除
        if ($id) {
            Model::name($this->menu)->where('id='.$id)->delete();
            Model::name($this->menu)->where('pid='.$id)->delete();
            return true;
        }
        return false;
    }

    /**
     * 單項列表
     * @param array $single 
     * @param array $where 
     */ 
    public function single($where=null,$page=1,$page_size=10){
        //單項列表
        $single['datalist'] = Model::name($this->single)
            ->alias('a')
            ->field(' a.*')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('id desc')
            ->select()
            ->toArray();
        //数据总数
        $single['total']=Model::name($this->single)
            ->where($where)
            ->count('id');
        return $single;

    }

    /**
     * 單項列表
     * @param array $singlelist 
     * @param array $where 
     */ 
    public function singlelist($where=null,$file='*'){ 
        return Model::name($this->single)
            ->field($file)
            ->where($where)
            ->select()
            ->toArray(); 
    }

    /**
     * 單項添加
     * @param array $singleadd 
     * @param array $where 
     */ 
    public function singleadd($id=null){ 
        $info = Model::name($this->single)->where([
            'id'=>$id
        ])->find(); 
        $info['langinfo']=[];
        if(isset($info['introduction'])){
            $info['introduction']=json_decode($info['introduction'],true);  
        }
        if(isset($info['titles'])){
            $info['titles']=json_decode($info['titles'],true);
            foreach ($info['titles'] as $key => $value) {
                 $info['langinfo'][$key]['titles']=$value;
                 $info['langinfo'][$key]['introduction']=$info['introduction'][$key];
             }     
        }
        unset($info['introduction'],$info['titles']);
        return $info;
    }

    /**
     * 單項保存
     * @param array $singlesave 
     * @param array $where 
     */ 
    public function singlesave($data){
        $id = !empty($data['id'])?$data['id']:false;
        unset($data['id']);
        $titles=[];
        $introduction=[];
        if(isset($data['langs'])&&!empty($data['langs'])){
            foreach ($data['langs'] as $key => $value) {
                $titles[$value]=$data['titles'][$key];
            }
            foreach ($data['langs'] as $key => $value) {
                $introduction[$value]=$data['introduction'][$key];
            }
        };
        if($data['type']==1){$data['cssid']=$data['dcssid']; }else{$data['cssid']=$data['lcssid'];}
        $data=[
                'title'=>$data['title'],
                'titles'=>json_encode($titles,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
                'introduction' =>json_encode($introduction,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
                'acode'=>$data['acode'],
                'imgurl'=>$data['imgurl'],
                'videourl'=>$data['videourl'],
                'is_video'=>$data['is_video'],
                'orderid'=>$data['orderid'],
                'status'=>$data['status'],
                'url'=>$data['url'],
                'type'=>$data['type'],
                'cssid'=>$data['cssid'],
                'is_target'=>$data['is_target'],
                'is_index'=>$data['is_index'],
                'sid'=>$data['sid'],
            ];
        if ($id){
            Model::name($this->single)->where(' id=' . $id)->update($data);
            $pid=Model::name($this->single)->where(' id=' . $id)->value('pid');
            if($pid>0){
                switch ($data['type']) {
                    case 1:
                        Model::name($this->contractcate)->where(' id=' . $pid)->update(['acode'=>$data['acode']]);
                        break;
                    
                    case 2:
                        Model::name($this->articleclass)->where(' id=' . $pid)->update(['acode'=>$data['acode']]);
                        break;
                }
            }
            return true; 
        } else {
            $id = Model::name($this->single)->insert($data);
            if($id){
                switch ($data['type']) {
                    case 1:
                        $data=[
                            'title'=>$data['title'],
                            'titles'=>json_encode($titles,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
                            'acode'=>$data['acode'],
                            'type'=>3,
                            'status'=>0
                        ];
                        $pid=Model::name($this->contractcate)->insert($data);
                        Model::name($this->single)->where(' id=' . $id)->update(['pid'=>$pid]);
                        break;
                    
                    case 2:
                        $data=[
                            'title'=>$data['title'],
                            'titles'=>json_encode($titles,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
                            'acode'=>$data['acode'],
                            'url'=>$data['url'],
                            'is_target'=>$data['is_target'],
                            'status'=>0
                        ];
                        $pid=Model::name($this->articleclass)->insert($data);
                        Model::name($this->single)->where(' id=' . $id)->update(['pid'=>$pid]);
                        break;
                }
            }
            return true; 
        }
        return false;
    }

    /**
     * 單項删除
     * @param array $singledel 
     * @param array $where 
     */
    public function singledel($id=null){
        //删除
        if ($id) {
            $acode=$this->singleadd($id);
            Model::name($this->single)->where('id='.$id)->delete();
            Model::name($this->contractcate)->where('acode="'.$acode['acode'].'"')->delete();
            return true; 
        }
        return false;
    }

	
}